#include<my_imgui.h>
#include <imgui.h>
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
#include <stdio.h>
#if defined(IMGUI_IMPL_OPENGL_ES2)
#include <GLES2/gl2.h>
#endif
#include <GLFW/glfw3.h> // Will drag system OpenGL headers

// [Win32] Our example includes a copy of glfw3.lib pre-compiled with VS2010 to maximize ease of testing and compatibility with old VS compilers.
// To link with VS2010-era libraries, VS2015+ requires linking with legacy_stdio_definitions.lib, which we do using this pragma.
// Your own project should not be affected, as you are likely to link with a newer binary of GLFW that is adequate for your version of Visual Studio.
#if defined(_MSC_VER) && (_MSC_VER >= 1900) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS)
#pragma comment(lib, "legacy_stdio_definitions")
#endif


static inline void load_imgui_data(Viewer_Opengl_Interpreter_Shader_Program*voisp)
{
    const char* glsl_version = "#version 430";

    IMGUI_CHECKVERSION();
    ImGui::CreateContext();
    ImGuiIO& io = ImGui::GetIO(); (void)io;
    //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;     // Enable Keyboard Controls
    //io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;      // Enable Gamepad Controls

    // Setup Dear ImGui style
    ImGui::StyleColorsDark();
    //ImGui::StyleColorsLight();

    // Setup Platform/Renderer backends
    GLFWwindow* window=(GLFWwindow*)(voisp->vw->g_info->window);
    ImGui_ImplGlfw_InitForOpenGL(window, true);
    ImGui_ImplOpenGL3_Init(glsl_version);

}

typedef struct My_imgui_init_s{
    void (*render_imgui)(void* res) ;
    void* prop;

}My_imgui_init_s;


static void render_imgui_data(Viewer_Opengl_Interpreter_Shader_Program*voisp)
{   
    My_imgui_init_s* miis=(My_imgui_init_s*)(voisp->prop);
    miis->render_imgui(miis->prop);

  

    voisp->vw->g_info->is_imgui_ui=ImGui::GetIO().WantCaptureMouse;

    ImGui::Render();

    //  glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w);
    // glClear(GL_COLOR_BUFFER_BIT);
    ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());


}

void my_viewer_opengl_interpreter_initn(Viewer_Opengl_Interpreter*voi,Viewer_World* vw, void(* render_imgui)(void* res),void* res )
{
    
    My_imgui_init_s* miis=(My_imgui_init_s*)malloc(sizeof(My_imgui_init_s));
    miis->render_imgui=render_imgui;
    miis->prop=res;
    Viewer_Opengl_Interpreter_Shader_Program* voisp=voi->create_shader_program_from_file(voi,NULL,NULL,load_imgui_data,render_imgui_data,vw);
    voisp->prop=miis;
    //viewer_opengl_interpreter_initn(voi,vwm);

    // RB_Tree* tree=vwm->container;
    // //RB_Tree_Trav*it=tree->begin(tree);
    // for(RB_Tree_Trav it=tree->begin(tree);it.it!=NULL;it.next(&it))
    // {
    //     Viewer_World* vw=(Viewer_World*)(it.second(&it));
    //     //viewer_opengl_interpreter_create_default_shader_program_from_world(voi,vw);
    // }
   
}